package com.lmu.travel.service.impl;

import com.github.wxpay.sdk.WXPay;
import com.lmu.travel.service.WXPayService;
import com.lmu.travel.util.PayConfig;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class WXPayServiceImpl implements WXPayService {


    @Override
    public Map<String, String> createNative(String out_trade_no, String total_fee, String goods) throws Exception {
        PayConfig config = new PayConfig();
        WXPay wxPay = new WXPay(config);

        Map<String, String> data = new HashMap<String, String>();
        data.put("body", "黑马旅游网");
        data.put("out_trade_no", out_trade_no);
        data.put("device_info", "");
        data.put("fee_type", "CNY");
        data.put("total_fee", total_fee);
        data.put("spbill_create_ip", "127.0.0.1");
        data.put("notify_url", "http://www.example.com/wxpay/notify");
        data.put("trade_type", "NATIVE");  // 此处指定为扫码支付
        data.put("product_id", goods);
        System.out.println("微信支付服务金额：resullt=" + total_fee);
        try {
            Map<String, String> result = wxPay.unifiedOrder(data);
            System.out.println("微信支付服务：resullt=" + result);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    //查询订单状态
    @Override
    public Map<String, String> queryNative(String out_trade_no) {
        PayConfig config = new PayConfig();
        WXPay wxPay = new WXPay(config);
        Map<String, String> data = new HashMap<String, String>();
        data.put("out_trade_no", out_trade_no);
        try {
            return wxPay.orderQuery(data);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    //关闭订单
    @Override
    public Map<String, String> closeNative(String out_trade_no) {
        PayConfig config = new PayConfig();
        WXPay wxPay = new WXPay(config);
        Map<String, String> data = new HashMap<String, String>();
        data.put("out_trade_no", out_trade_no);
        try {
            return wxPay.closeOrder(data);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
